package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Destination;
import cn.wolfcode.domain.Travel;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.query.TravelCondition;
import cn.wolfcode.query.TravelQuery;
import cn.wolfcode.repository.TravelRepository;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.ITravelService;
import cn.wolfcode.service.IUserInfoService;
import cn.wolfcode.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {



    @Autowired
    private TravelRepository travelRepository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IDestinationService destinationService;

    @Override
    public void save(Travel travel) {
        travel.setId(null);
        travelRepository.save(travel);
    }

    @Override
    public void update(Travel travel) {
        travelRepository.save(travel);
    }

    @Override
    public void delete(String id) {
        travelRepository.deleteById(id);
    }

    @Override
    public Travel get(String id) {

        Travel travel = travelRepository.findById(id).orElse(null);
        if(travel != null){
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            travel.setDest(destinationService.get(travel.getDestId()));
        }

        return travel;

    }

    @Override
    public List<Travel> findAll() {
        return travelRepository.findAll();
    }

    @Override
    public Page<Travel> query(TravelQuery qo) {
        //travelRepository 查询只能对所有数据进行分页
        //template 查询不能直接对数据进行分页 间接分页

        Query query = new Query();

        if(StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }

        //出行时间
        TravelCondition day = qo.getDay();
        if(day != null){
            query.addCriteria(Criteria.where("day").gte(day.getMin()).lte(day.getMax()));
        }

        TravelCondition perExpend = qo.getPerExpend();
        if(perExpend != null){
            query.addCriteria(Criteria.where("perExpend").gte(perExpend.getMin()).lte(perExpend.getMax()));
        }

        //totalCount 总记录数
        long totalCount = template.count(query, Travel.class);
        if(totalCount == 0){
            return  Page.empty();  //空的分页对象
        }
        Pageable pageable;
        if(qo.getOrderType() == 1){
            pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "createTime");
        }else if (qo.getOrderType() == 2){
            pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "viewnum");
        } else{
            //分页参数对象, 参数1 当前页  2 每页显示的条数  3 排序规则  4 排序字段
            pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        }


        Page<Travel> page = DBHelper.query(template, Travel.class, query, pageable);
        for (Travel travel : page) {
            travel.setAuthor( userInfoService.get(travel.getUserId()));
        }

//        query.with(pageRequest);
//        List<Travel> list = template.find(query, Travel.class);

        return page;
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        //目的地名称
        Destination destination = destinationService.get(travel.getDestId());
        travel.setDestName(destination.getName());
        //用户id


        //最后更新时间
        travel.setLastUpdateTime(new Date());
        if(StringUtils.hasLength(travel.getId())){
            //有id修改
            //参数个数一致等同于全量更新
            travel.setCreateTime(new Date());
            this.update(travel);

        }else{
            this.save(travel);
        }
    }

    @Override
    public void audit(String id, int state) {
        //判断是否满足审核条件     通过之后需要做啥   拒绝之后需要做啥
        Travel travel = this.get(id);
        if(travel == null){
            throw new LogicException("参数异常");
        }

        if(travel.getState() != Travel.STATE_WAITING){
            throw new LogicException("不允许审核");
        }

        if(state == Travel.STATE_RELEASE){
            //修改状态为通过审核状态  修改发布时间  最后更新时间  记录审核信息
            travel.setState(Travel.STATE_RELEASE);
            travel.setReleaseTime(new Date());
            travel.setLastUpdateTime(new Date());
            this.update(travel);
        }else if (state == Travel.STATE_REJECT){
            //修改状态为通过审核状态  修改发布时间为null
            travel.setState(Travel.STATE_REJECT);
            travel.setReleaseTime(new Date());
            travel.setLastUpdateTime(new Date());
            this.update(travel);

        }else{
            throw new LogicException("状态不对");
        }


    }

    @Override
    public List<Travel> queryViewnumTop3(String destId) {
        Pageable pageable = PageRequest.of(0,3, Sort.Direction.DESC,"viewnum");
        List<Travel> list = travelRepository.findByDestId(destId, pageable);
        return list;
    }

    @Override
    public List<Travel> getByDestName(String destName) {
        List<Travel> name = travelRepository.findByDestName(destName);
        for (Travel travel : name) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            travel.setDest(destinationService.get(travel.getDestId()));
        }
        return name;
    }

}
